package com.souku.finance.application.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.souku.finance.application.R;
import com.souku.finance.application.adapter.RecordAdapter;
import com.souku.finance.application.adapter.ViewPagerAdapter;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.bean.RecordNotBean;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.widget.ViewPagerIndicator;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.SuccessError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Android on 2016/12/20.
 */

public class InvestRecordActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {


    protected ViewPagerIndicator idIndicator;

    private PullToRefreshListView alreadyrecord_listview;
    private RecordAdapter alreadyrecordAdapter;
    private int pageAlready = 1;
    private List<RecordNotBean.DataBean> listhas = new ArrayList<>();

    private PullToRefreshListView notrecord_listview;
    private RecordAdapter notrecordAdapter;
    private int pageNot = 1;
    private List<RecordNotBean.DataBean> listno = new ArrayList<>();

    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected ViewPager idVp;
    private View view1;
    private View view2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_investrecord);
        initView();
        view1 = LayoutInflater.from(this).inflate(R.layout.page_notrecord, null);
        view2 = LayoutInflater.from(this).inflate(R.layout.page_alreadyrecord, null);
        final ArrayList<View> views = new ArrayList<>();
        views.add(view1);
        views.add(view2);
        ViewPagerAdapter adapter = new ViewPagerAdapter(views);
        idVp.setAdapter(adapter);
        topTitle.setText("我的投资");

        notrecord_listview = (PullToRefreshListView) view1.findViewById(R.id.notrecord_pull_list);
        notrecordAdapter = new RecordAdapter(listno);
        notrecord_listview.setMode(PullToRefreshBase.Mode.BOTH);
        notrecord_listview.setAdapter(notrecordAdapter);
        notrecord_listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageNot = 1;
                listno.clear();
                getNotInvitation();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageNot++;
                getNotInvitation();
            }
        });
        notrecord_listview.setOnItemClickListener(this);

        alreadyrecord_listview = (PullToRefreshListView) view2.findViewById(R.id.alreadyrecord_pull_list);
        alreadyrecordAdapter = new RecordAdapter(listhas);
        alreadyrecord_listview.setMode(PullToRefreshBase.Mode.BOTH);
        alreadyrecord_listview.setAdapter(alreadyrecordAdapter);
        alreadyrecord_listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageAlready = 1;
                listhas.clear();
                getAlreadyInvitation();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageAlready++;
                getAlreadyInvitation();
            }
        });
        alreadyrecord_listview.setOnItemClickListener(this);
        idIndicator.setViewPager(idVp, 0);
        notrecord_listview.setRefreshing(true);
        alreadyrecord_listview.setRefreshing(true);
    }

    @Override
    public void internet() {
        notrecord_listview.setRefreshing(true);
        alreadyrecord_listview.setRefreshing(true);
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            finish();
        }
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(InvestRecordActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        idVp = (ViewPager) findViewById(R.id.id_vp);
        idIndicator = (ViewPagerIndicator) findViewById(R.id.id_indicator);
    }

    private void getAlreadyInvitation() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("hasRepayed", "1");
        map.put("page", pageAlready + "");
        RequestManager.getInstance().post(UrlConstact.REPAYDE_LIST, map, new ReqListener(this, new SuccessError() {
            @Override
            public void onSuccess(String response) {
                alreadyrecord_listview.onRefreshComplete();
                if (JsonUtil.getCode(response) == 1) {
                    RecordNotBean bean = JsonUtil.fastBean(response, RecordNotBean.class);
                    if (bean.getData().size() == 0) {
                        if (pageAlready != 1) {
                            ToastUtil.show("没有更多内容");
                        }
                    } else {
                        listhas.addAll(bean.getData());
                        alreadyrecordAdapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                alreadyrecord_listview.onRefreshComplete();
                ToastUtil.show("请求失败");
            }
        }), ParamUtil.getHeaders());
    }

    private void getNotInvitation() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("hasRepayed", "0");
        map.put("page", pageNot + "");
        RequestManager.getInstance().post(UrlConstact.REPAYDE_LIST, map, new ReqListener(this, new SuccessError() {
            @Override
            public void onSuccess(String response) {
                notrecord_listview.onRefreshComplete();
                if (JsonUtil.getCode(response) == 1) {
                    RecordNotBean bean = JsonUtil.fastBean(response, RecordNotBean.class);
                    if (bean.getData().size() == 0) {
                        if (pageNot != 1) {
                            ToastUtil.show("没有更多内容");
                        }
                    } else {
                        listno.addAll(bean.getData());
                        notrecordAdapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                notrecord_listview.onRefreshComplete();
                ToastUtil.show("请求失败");
            }
        }), ParamUtil.getHeaders());
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String deailId = (String) parent.getItemAtPosition(position);
        Intent intent = new Intent(this, InvestRecordDetailsActivity.class);
        intent.putExtra(Constant.Default, deailId);
        startActivity(intent);
    }
}
